<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.png">
  <link rel="mask-icon" href="/images/avatar.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"notes.maxwi.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":true,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="网上关于Linux中动态链接库和动态链接库的博客有很多，其实大部分博客都没有分清楚linux下面动态链接库运行时的查找路径跟编译期的查找路径并不相同，导致很多人在编译程序时库引用出错，按教程添加库路径后依然出错。下面一并总结，不涉及太多二进制中ELF的相关内容。Linux系统中有大量共用的动态链接库和静态链接库，当对程序源代码编译之后进行链接时往往需要引用系统中已经有的动态链接库或静态链接库。关于">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux动态链接库以及链接器相关知识">
<meta property="og:url" content="http://notes.maxwi.com/2017/04/13/linux-dynamic-lib-ldconfig/index.html">
<meta property="og:site_name" content="blueyi&#39;s notes">
<meta property="og:description" content="网上关于Linux中动态链接库和动态链接库的博客有很多，其实大部分博客都没有分清楚linux下面动态链接库运行时的查找路径跟编译期的查找路径并不相同，导致很多人在编译程序时库引用出错，按教程添加库路径后依然出错。下面一并总结，不涉及太多二进制中ELF的相关内容。Linux系统中有大量共用的动态链接库和静态链接库，当对程序源代码编译之后进行链接时往往需要引用系统中已经有的动态链接库或静态链接库。关于">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2017-04-13T07:22:28.000Z">
<meta property="article:modified_time" content="2017-04-13T07:22:28.000Z">
<meta property="article:author" content="blueyi">
<meta property="article:tag" content="Linux">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://notes.maxwi.com/2017/04/13/linux-dynamic-lib-ldconfig/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>Linux动态链接库以及链接器相关知识 | blueyi's notes</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="blueyi's notes" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="blueyi's notes" type="application/rss+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">blueyi's notes</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Follow Excellence,Success will chase you!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://notes.maxwi.com/2017/04/13/linux-dynamic-lib-ldconfig/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/default_avatar.jpg">
      <meta itemprop="name" content="blueyi">
      <meta itemprop="description" content="心怀善意，虛怀若谷！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="blueyi's notes">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Linux动态链接库以及链接器相关知识
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2017-04-13 15:22:28" itemprop="dateCreated datePublished" datetime="2017-04-13T15:22:28+08:00">2017-04-13</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Linux/" itemprop="url" rel="index"><span itemprop="name">Linux</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="Symbols count in article">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">Symbols count in article: </span>
              <span>4.2k</span>
            </span>
            <span class="post-meta-item" title="Reading time">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">Reading time &asymp;</span>
              <span>4 mins.</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>网上关于Linux中动态链接库和动态链接库的博客有很多，其实大部分博客都没有分清楚linux下面动态链接库运行时的查找路径跟编译期的查找路径并不相同，导致很多人在编译程序时库引用出错，按教程添加库路径后依然出错。下面一并总结，不涉及太多二进制中ELF的相关内容。<br>Linux系统中有大量共用的动态链接库和静态链接库，当对程序源代码编译之后进行链接时往往需要引用系统中已经有的动态链接库或静态链接库。关于动态链接库可以简单理解为在链接时只是将动态库中需要引用的内容（包括变量、函数）在目标程序的符号表中创建一个链接，只在运行到该部分调用时才去查看动态链接库，linux下动态链接库通常为<code>libxxx.so</code>，windows下为<code>.dll</code>。而静态链接库是将整个库一并打包到链接后的目标程序中，并直接映射好所需要的符号表，静态链接库后缀通常为<code>.a</code>，windows下通常为<code>.lib</code>。如果也不明白符号表，可以参考<a href="http://notes.maxwi.com/2016/06/05/source-to-program/">实例验证C/C++源代码变成程序的过程</a></p>
<span id="more"></span>

<p>Linux下有两个很好用的实用程序：</p>
<ul>
<li><code>ldd</code>：用于查看共享库依赖信息</li>
<li><code>ldconfig</code>：用于配置动态链接器的运行时绑定</li>
</ul>
<p>还有两个Linux下非常重要的动态链接器/载入器：</p>
<ul>
<li><code>ld.so</code></li>
<li><code>ld-linux.so*</code></li>
</ul>
<p>以及GNU的链接器：</p>
<ul>
<li><code>ld</code>：这个就是将一堆的目标文件通过符号表链接成最终的程序，可以参考上面推荐的另一篇文章</li>
</ul>
<h2 id="ldd"><a href="#ldd" class="headerlink" title="ldd"></a>ldd</h2><p>通过ldd命令后面直接跟文件名即可，不仅可以查看可执行二进制程序的依赖库，也可以查看库本身的依赖，如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ldd /lib/x86_64-linux-gnu/libm.so.6</span><br></pre></td></tr></table></figure>
<p>输出为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">linux-vdso.so.1 =&gt;  (0x00007ffd3afe6000)</span><br><span class="line">libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f35df5f5000)</span><br><span class="line">/lib64/ld-linux-x86-64.so.2 (0x000055a75e17f000)</span><br></pre></td></tr></table></figure>

<p>查看ls的依赖库：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ldd /bin/ls</span><br></pre></td></tr></table></figure>
<p>输出为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">linux-vdso.so.1 =&gt;  (0x00007ffc6492b000)</span><br><span class="line">libselinux.so.1 =&gt; /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f5128ace000)</span><br><span class="line">libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5128705000)</span><br><span class="line">libpcre.so.3 =&gt; /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f5128494000)</span><br><span class="line">libdl.so.2 =&gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5128290000)</span><br><span class="line">/lib64/ld-linux-x86-64.so.2 (0x0000563eda70b000)</span><br><span class="line">libpthread.so.0 =&gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f5128073000)</span><br></pre></td></tr></table></figure>

<p><strong>对于运行时或链接时可能会出现的<code>undefined symbol</code>错误可以通过<code>ldd -r &lt;libxx.so&gt;</code>来查看其有哪些符号未定义，它会输出很详细的相关符号的名字</strong>，如：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">$ ldd -r /usr/lib/libGL.so</span><br><span class="line">        linux-gate.so.1 =&gt;  (0xf77a5000)</span><br><span class="line">        libdl.so.2 (0xf771c000)</span><br><span class="line">        libGLX.so.0 (0xf76fd000)</span><br><span class="line">        libGLdispatch.so.0 (0xf767d000)</span><br><span class="line">        libc.so.6 (0xf74bc000)</span><br><span class="line">        /lib/ld-linux.so.2 (0xf77a6000)</span><br><span class="line">        libX11.so.6 =&gt; not found</span><br><span class="line">        libXext.so.6 =&gt; not found</span><br><span class="line">undefined symbol: _XAsyncErrorHandler   (libGLX.so.0)</span><br><span class="line">undefined symbol: _XFlush       (libGLX.so.0)</span><br><span class="line">undefined symbol: XScreenCount  (libGLX.so.0)</span><br><span class="line">undefined symbol: _XReply       (libGLX.so.0)</span><br><span class="line">undefined symbol: XFree (libGLX.so.0)</span><br><span class="line">undefined symbol: _XDeqAsyncHandler     (libGLX.so.0)</span><br><span class="line">undefined symbol: _XError       (libGLX.so.0)</span><br><span class="line">undefined symbol: XQueryExtension       (libGLX.so.0)</span><br><span class="line">undefined symbol: _XRead        (libGLX.so.0)</span><br><span class="line">undefined symbol: XESetCloseDisplay     (libGLX.so.0)</span><br><span class="line">undefined symbol: XAddExtension (libGLX.so.0)</span><br><span class="line">undefined symbol: _XEatData     (libGLX.so.0)</span><br></pre></td></tr></table></figure>

<p>通过这个名字可以知道这个是为了支持C++重载被编译器mangle之后的函数引用名字，如果是C语言引用C++的库中的函数，此处的库函数引用应该加上<code>extern &quot;C&quot;</code>.</p>
<p><strong>也可以使用<code>nm -A &lt;libxxx.so&gt;</code>来查看符号表</strong></p>
<p>更多ldd功能查看<code>man ldd</code></p>
<h2 id="ldconfig"><a href="#ldconfig" class="headerlink" title="ldconfig"></a>ldconfig</h2><p>ldconfig管理的库路径通常有<code>/usr/lib</code>、<code>/lib</code>、<code>/usr/lib64</code>、<code>/lib64</code>以及<code>/etc/ld.so.conf</code>中指定的所有路径。<br>ldconfig常用的功能如下：</p>
<h3 id="更新动态链接库缓存"><a href="#更新动态链接库缓存" class="headerlink" title="更新动态链接库缓存"></a>更新动态链接库缓存</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo ldconfig</span><br></pre></td></tr></table></figure>
<p>当安装完新库或者在<code>/etc/ld.so.conf</code>，或者<code>/etc/ld.so.conf.d/*.conf</code>中添加库路径时都需要运行<code>ldconfig</code>来更新缓存使新添加的库生效。当然系统启动时会自动运行<code>ldconfig</code>，因为好多软件重装完成之后都会建议重启电脑。</p>
<h3 id="查看ld搜索到的所有库路径以及创建的库链接"><a href="#查看ld搜索到的所有库路径以及创建的库链接" class="headerlink" title="查看ld搜索到的所有库路径以及创建的库链接"></a>查看ld搜索到的所有库路径以及创建的库链接</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo ldconfig -v</span><br></pre></td></tr></table></figure>
<p><code>-v</code>参数也就是<code>--verbose</code>模式，很多人称为开启啰嗦模式也很贴切</p>
<h3 id="只输入动态链接库搜索路径"><a href="#只输入动态链接库搜索路径" class="headerlink" title="只输入动态链接库搜索路径"></a>只输入动态链接库搜索路径</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo ldconfig -v 2&gt;/dev/null | grep -v ^$&#x27;\t&#x27;</span><br></pre></td></tr></table></figure>

<h3 id="只查看当前缓存中的库"><a href="#只查看当前缓存中的库" class="headerlink" title="只查看当前缓存中的库"></a>只查看当前缓存中的库</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo ldconfig -p</span><br></pre></td></tr></table></figure>

<h2 id="链接器"><a href="#链接器" class="headerlink" title="链接器"></a>链接器</h2><h3 id="程序运行时库查找路径"><a href="#程序运行时库查找路径" class="headerlink" title="程序运行时库查找路径"></a>程序运行时库查找路径</h3><p><code>ld.so</code>和<code>ld-linux.so</code>可以在程序运行前搜索所需要支持库路径，它们的库搜索路径除了在ldconfig中提到的之外，还包含通过环境变量<code>LD_LIBRARY_PATH</code>指定的路径。<br>所以当你的程序在运行时提示缺少库，可以通过将所需要的的动态库添加到该环境变量中，或者加入到<code>/etc/ld.so.conf</code>中，然后采用ldconfig更新库缓存。<br>更多内容可以<code>man ld.so</code></p>
<h3 id="编译期链接库路径"><a href="#编译期链接库路径" class="headerlink" title="编译期链接库路径"></a>编译期链接库路径</h3><p>当通过GNU的gcc或g++编译并链接程序时（下面举例中使用的gcc，g++情况类似），其实链接部分的功能跟刚才提到的链接器<code>ld</code>一样，甚至很多时候就是调用系统中的<code>ld</code>。在将目标文件链接成可执行的二进制程序时搜索的库路径通常都是系统库路径，注意与程序运行时载入的动态库路径不一样，所以无论你怎么修改运行时搜索库目录，编译依然会失败，可以通过以下方式查看链接器ld的库搜索路径：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ld --verbose | grep SEARCH_DIR | tr -s &#x27; ;&#x27; \\012</span><br></pre></td></tr></table></figure>

<p>或者通过gcc的啰嗦模式查看，gcc会比ld多搜索通过<code>-L</code>参数指定的库路径，以及环境变量<code>LIBRARY_PATH</code>中指定的路径：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc -print-search-dirs | sed &#x27;/^lib/b 1;d;:1;s,/[^/.][^/]*/\.\./,/,;t 1;s,:[^=]*=,:;,;s,;,;  ,g&#x27; | tr \; \\012</span><br></pre></td></tr></table></figure>

<p>所以当你在编译并链接程序时如果出现库缺少的情况，可以通过<code>-L</code>来指定所需要的库，gcc的<code>-L</code>用法为<code>-Ldir</code>，或者将其加到环境变量<code>LIBRARY_PATH</code>中，注意前面没有<em>LD_*。静态库可以通过<code>-llibrary</code>或<code>-l library</code>指定。<br>*注意这里跟通过<code>-I</code>参数指定头文件搜索路径不同，gcc的<code>-Idir</code>用来指定头文件搜索路径。当然也可以通过以下环境变量来为gcc或g++添加头文件搜索路径：</em></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">export CPLUS_INCLUDE_PATH=/usr/lib/jvm/java-8-oracle/include/:/usr/lib/jvm/java-8-oracle/include/linux/:$CPLUS_INCLUDE_PATH</span><br><span class="line">export C_INCLUDE_PATH=/usr/lib/jvm/java-8-oracle/include/:/usr/lib/jvm/java-8-oracle/include/linux/:$C_INCLUDE_PATH</span><br></pre></td></tr></table></figure>
<p><em>上面那行用于g++，下面那行用于gcc，本例是添加java的jni头文件路径</em></p>
<p><strong>关于<code>LD_LIBRARY_PATH</code>和<code>LD_RUN_PATH</code>的区别，下面这两句来自gcc的make install提示很棒</strong></p>
<ul>
<li>add LIBDIR to the <code>LD_LIBRARY_PATH</code> environment variable during execution</li>
<li>add LIBDIR to the <code>LD_RUN_PATH</code> environment variable during linking</li>
</ul>
<p>更多内容可以<code>man ld</code>、<code>man gcc</code></p>
<h3 id="gcc编译时verbose选项"><a href="#gcc编译时verbose选项" class="headerlink" title="gcc编译时verbose选项"></a>gcc编译时verbose选项</h3><p>使用gcc编译时可以通过添加<code>-v</code>或者<code>--verbose</code>来开启啰嗦模式，查看链接器的调用情况，但并不会显示链接器的啰嗦模式的输出内容。</p>
<p>可以通过<code>-Wl</code>向链接器传递参数的方式来查看链接器的啰嗦模式信息输出情况，例如<code>-Wl,--verbose</code>，gcc的<code>-Wl</code>的用法为<code>-Wl,option</code>，其中option为要传递给链接器的参数。</p>
<p>更多内容可以<code>man gcc</code></p>
<p>参考：<br>1.<a target="_blank" rel="noopener" href="http://stackoverflow.com/questions/9922949/how-to-print-the-ldlinker-search-path">How to print the ld(linker) search path</a></p>
<p>2.<a target="_blank" rel="noopener" href="http://stackoverflow.com/questions/4250624/ld-library-path-vs-library-path">LD_LIBRARY_PATH vs LIBRARY_PATH</a><br>当然还有一堆的man手册</p>

    </div>

    
    
    
        

  <div class="followme">
    <p>Welcome to my other publishing channels</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Linux/" rel="tag"># Linux</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2017/04/05/ffmpeg-codecs-formats/" rel="prev" title="FFmpeg支持的压缩格式和编解码格式">
      <i class="fa fa-chevron-left"></i> FFmpeg支持的压缩格式和编解码格式
    </a></div>
      <div class="post-nav-item">
    <a href="/2017/04/19/java-jni-cpp-hello/" rel="next" title="Java通过JNI调用C/C++动态链接库之HelloWorld">
      Java通过JNI调用C/C++动态链接库之HelloWorld <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#ldd"><span class="nav-number">1.</span> <span class="nav-text">ldd</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ldconfig"><span class="nav-number">2.</span> <span class="nav-text">ldconfig</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E5%8A%A8%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93%E7%BC%93%E5%AD%98"><span class="nav-number">2.1.</span> <span class="nav-text">更新动态链接库缓存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8Bld%E6%90%9C%E7%B4%A2%E5%88%B0%E7%9A%84%E6%89%80%E6%9C%89%E5%BA%93%E8%B7%AF%E5%BE%84%E4%BB%A5%E5%8F%8A%E5%88%9B%E5%BB%BA%E7%9A%84%E5%BA%93%E9%93%BE%E6%8E%A5"><span class="nav-number">2.2.</span> <span class="nav-text">查看ld搜索到的所有库路径以及创建的库链接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%AA%E8%BE%93%E5%85%A5%E5%8A%A8%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93%E6%90%9C%E7%B4%A2%E8%B7%AF%E5%BE%84"><span class="nav-number">2.3.</span> <span class="nav-text">只输入动态链接库搜索路径</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%AA%E6%9F%A5%E7%9C%8B%E5%BD%93%E5%89%8D%E7%BC%93%E5%AD%98%E4%B8%AD%E7%9A%84%E5%BA%93"><span class="nav-number">2.4.</span> <span class="nav-text">只查看当前缓存中的库</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%93%BE%E6%8E%A5%E5%99%A8"><span class="nav-number">3.</span> <span class="nav-text">链接器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A8%8B%E5%BA%8F%E8%BF%90%E8%A1%8C%E6%97%B6%E5%BA%93%E6%9F%A5%E6%89%BE%E8%B7%AF%E5%BE%84"><span class="nav-number">3.1.</span> <span class="nav-text">程序运行时库查找路径</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%96%E8%AF%91%E6%9C%9F%E9%93%BE%E6%8E%A5%E5%BA%93%E8%B7%AF%E5%BE%84"><span class="nav-number">3.2.</span> <span class="nav-text">编译期链接库路径</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gcc%E7%BC%96%E8%AF%91%E6%97%B6verbose%E9%80%89%E9%A1%B9"><span class="nav-number">3.3.</span> <span class="nav-text">gcc编译时verbose选项</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="blueyi"
      src="/images/default_avatar.jpg">
  <p class="site-author-name" itemprop="name">blueyi</p>
  <div class="site-description" itemprop="description">心怀善意，虛怀若谷！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">104</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">68</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/blueyi" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;blueyi" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://maxwi.com/" title="http:&#x2F;&#x2F;maxwi.com" rel="noopener" target="_blank">Maxwi</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2014 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">blueyi</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="Symbols count total">750k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="Reading time total">11:22</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '0f8243eb2c8b2207980f',
      clientSecret: 'd159633a33519d3b7a48b0ca729032f7d1f38a41',
      repo        : 'notes',
      owner       : 'blueyi',
      admin       : ['blueyi'],
      id          : '66070c259fbf8743215ed2fae29d99a7',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

    </div>
</body>
</html>
